<?php

namespace app\controllers\admin\uni\task;

use app\controllers\admin\base\AdminController;
use app\models\task\Task;
use app\models\task\TaskList;
use app\models\task\TaskList as Model;
use app\models\user\UserAccount;
use app\models\user\UserCommission;

class CheckController extends AdminController
{
    public function actionList()
    {
        $user_id = $this->post('user_id');
        $id = $this->post('id');
        $status = $this->post('status');
        $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        $query = Model::find()
            ->with(['images.image', 'user', 'task']);

        if ($status > Model::$statusMapping['ALL']) {
            $query->andWhere(['status' => $status]);
        }

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC]);
        }

        if ($id) {
            $query->andWhere(['id' => $id]);
        }
        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }


    public function actionStatusList()
    {
        return $this->success(Model::statusList());
    }

    // 审核
    public function actionCheck()
    {
        $id = $this->post('id');
        $model = TaskList::findOne(['id' => $id]);

        if ($model) {

            if ($model->status == TaskList::$statusMapping['FINISH']) {
                return $this->fail('请勿重复发放');
            }

            $task = Task::findOne(['id' => $model->task_id]);

            $model->status = TaskList::$statusMapping['FINISH'];
            $model->commission = $task->amount;
            $model->save();

            // $user_account = UserAccount::findOne(['user_id' => $model->user_id]);
            // $user_account->balance += $task->amount;
            // $user_account->save();

            $this->saveCommission($model->user_id, $task->amount, 30, '完成任务');

            return $this->success();
        }
    }

    // 驳回
    public function actionReject()
    {
        $id = $this->post('id');
        $reject = $this->post('reject');
        $model = TaskList::findOne(['id' => $id]);

        if ($model) {

            if ($model->status == TaskList::$statusMapping['FINISH']) {
                return $this->fail('已完成, 不能驳回');
            }

            $model->status = TaskList::$statusMapping['REJECT'];
            $model->reject = $reject;
            $model->save();

            return $this->success();
        }
    }

    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = 0;
            $model->order_id = 0;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                $account->save();
            }
        }
    }
}